package com.huawei.mpchartexample;

import com.huawei.mpchartlib.charts.BarChart;
import com.huawei.mpchartlib.components.XAxis;
import com.huawei.mpchartlib.components.YAxis;
import com.huawei.mpchartlib.data.BarData;
import com.huawei.mpchartlib.data.BarDataSet;
import com.huawei.mpchartlib.data.BarEntry;
import com.huawei.mpchartlib.interfaces.datasets.IBarDataSet;
import com.huawei.mpchartlib.utils.ColorTemplate;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.Slider;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Canvas;

import java.util.ArrayList;

public class BarChartAbility extends Ability implements Slider.ValueChangedListener{

    private BarChart chart;
    private Slider sliderX, sliderY;
    private Text tvX, tvY;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_barchart);

        tvX = findComponentById(ResourceTable.Id_tvX);
        tvY = findComponentById(ResourceTable.Id_tvY);

        sliderX = findComponentById(ResourceTable.Id_sliderX);
        sliderX.setValueChangedListener(this);

        sliderY = findComponentById(ResourceTable.Id_sliderY);
        sliderY.setValueChangedListener(this);

        chart = (BarChart) findComponentById(ResourceTable.Id_barChart);
        {   // // Chart Style // //

            // background color
            ShapeElement background= new ShapeElement();
            background.setShape(ShapeElement.RECTANGLE);
            background.setRgbColor(new RgbColor(255,255,255));
            chart.setBackground(background);

            // disable description text
            chart.getDescription().setEnabled(false);

            // enable touch gestures
            chart.setTouchEnabled(false);

            // set listeners
            chart.setOnChartValueSelectedListener(null);

            chart.setDrawBarShadow(false);
            chart.setDrawValueAboveBar(true);

            // if more than 60 entries are displayed in the chart, no values will be
            // drawn
            chart.setMaxVisibleValueCount(60);

            // scaling can now only be done on x- and y-axis separately
            chart.setPinchZoom(false);
        }

        XAxis xAxis;
        {   // // X-Axis Style // //
            xAxis = chart.getXAxis();
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            xAxis.setDrawGridLines(false);
        }

        // add data
        sliderX.setProgressValue(10);
        sliderY.setProgressValue(100);
        setData(10, 100);
        chart.invalidate();

        // draw chart
        Component.DrawTask drawTask = new Component.DrawTask() {
            @Override
            public void onDraw(Component component, Canvas canvas) {
                chart.onDraw(canvas);
            }
        };
        chart.addDrawTask(drawTask);
    }

    private void setData(int count, float range) {
        ArrayList<BarEntry> values = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            float val = (float) (Math.random() * range) + range / 3;
            values.add(new BarEntry(i, val));
        }

        BarDataSet set1;

        if (chart.getData() != null &&
                chart.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet) chart.getData().getDataSetByIndex(0);
            set1.setEntries(values);
            set1.notifyDataSetChanged();
            chart.getData().notifyDataChanged();
            chart.notifyDataSetChanged();
        } else {
            // create a dataset and give it a type
            set1 = new BarDataSet(values, "DataSet 1");

            // set bar colors
            set1.setColors(ColorTemplate.VORDIPLOM_COLORS);

            set1.setDrawValues(false);

            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(set1); // add the data sets

            // create a data object with the data sets
            BarData data = new BarData(dataSets);

            // set data
            chart.setData(data);

            chart.setFitBars(true);
        }
    }

    @Override
    public void onTouchStart(Slider slider) {

    }

    @Override
    public void onTouchEnd(Slider slider) {
    }

    @Override
    public void onProgressUpdated(Slider slider, int i, boolean b) {
        tvX.setText(String.valueOf(sliderX.getProgress()));
        tvY.setText(String.valueOf(sliderY.getProgress()));
        setData(sliderX.getProgress(), sliderY.getProgress());
        // redraw
        chart.invalidate();
    }
}
